package cn.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zgn
 * @Description: DbUtil
 * @date 2021/9/7 0007
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Mysql8DbUtil {
    //测试数据库 mysql 8.0.27
    /*public String url ="jdbc:mysql://127.0.0.1:3306/demo";
    public String name = "root";//数据库用户名
    public String password = "0000";//数据库密码*/

    //广新
    public String url ="jdbc:mysql://gidg.hcl.ink:10535/fee-center";
    public String name = "root";//数据库用户名
    public String password = "123123";//数据库密码

    /**
     * 该方法用于获取连接对象
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url,name,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 该方法用于释放资源
     */
    public void release(Connection conn,Statement st,ResultSet rs) {
        closeRs(rs);
        closeSt(st);
        closeConn(conn);
    }
    public void release(Connection conn,Statement st) {
        closeSt(st);
        closeConn(conn);
    }
    /**
     * 下划线变大写
     * @auther zgn
     * @date  2021/12/24 0024
     *
     * @param code 下划线字段
     * @return 大写字段
     **/
    public String getUrlByCode(String code) {
        char[] charArray = code.toCharArray();
        // A-Z 对应数字65-90 a-z 对应数字97-122
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == '_') {
                // 字符在97-122之间的都是小写字母，在原基础上减32转换成大写
                if (charArray[i + 1] >= 97 && charArray[i + 1] <= 122) {
                    int upper = charArray[i + 1] - 32;
                    charArray[i + 1] = (char) upper;
                }
            }
        }
        StringBuffer url = new StringBuffer("");
        for (int i = 0; i < charArray.length; i++) {
            url.append(charArray[i]);
        }
        return url.toString().replace("_", "");
    }

    private void closeRs(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            rs = null;
        }
    }
    private void closeSt(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            st = null;
        }
    }
    private void closeConn(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            conn = null;
        }
    }
}
